package org.ccu.eshop.dao;

import org.ccu.eshop.domain.dto.CategoryVO;
import org.ccu.eshop.utils.ComboPooledDataSourceUtil;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***
 * dao才是真正和数据库Category表交互的
 *
 */
public class CategoryDao {
    /**
     * 获得商品类别所有列表信息
     * @return
     */
    public List<CategoryVO> selectCategoryList() {

        List<CategoryVO> list=new ArrayList<>();

        //1-获得数据库的连接
        Connection connection= ComboPooledDataSourceUtil.getConnection();
        //2-操作数据库表
        try{

            //2-1获得承接sql的容器对象
            String sql="select id, cateName, cateDesc from  category";
            //2-2获得sql语句
            PreparedStatement  ps=connection.prepareStatement(sql);
            //2-3执行sql语句
            ResultSet set=ps.executeQuery();
            while (set.next()){
                CategoryVO category=new CategoryVO();
                category.setId(set.getInt("id"));
                category.setCateName(set.getString("cateName"));
                category.setCateDesc(set.getString("cateDesc"));
                list.add(category);
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //3关闭数据库连接
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
            return list;
    }
}
